[% setvar title C<@STACK> - a modifyable C<caller()> %]
<div id="archive-notice">
    <h3>This file is part of the Perl 6 Archive</h3>
    <p>To see what is currently happening visit <a href="http://www.perl6.org/">http://www.perl6.org/</a></p>
</div>
<div class='pod'>
<a name='TITLE'></a><h1>TITLE</h1>
<p><code>@STACK</code> - a modifyable <code>caller()</code></p>
<a name='VERSION'></a><h1>VERSION</h1>
<pre>  Maintainer: Simon Cozens &lt;<a href='mailto:simon@brecon.co.uk'>simon@brecon.co.uk</a>&gt;
  Date: 25 Sep 2000
  Mailing List: <a href='mailto:perl6-language-subs@perl.org'>perl6-language-subs@perl.org</a>
  Number: 299
  Version: 1
  Status: Developing</pre>
<a name='ABSTRACT'></a><h1>ABSTRACT</h1>
<p>Add a new special variable, <code>@STACK</code> to replace the <code>caller()</code>
function. Allow people to modify the call stack in certain, very
restricted ways.</p>
<a name='DESCRIPTION'></a><h1>DESCRIPTION</h1>
<p>Suppose you're writing a debugger; one thing you may want to do is
perform some actions when a subroutine ends. Well, if all ops are
overridable, no problem; you just override the <code>return</code> op. The only
problem here is that you can't write the <code>return</code> operation in Perl.</p>
<p>Don't believe me? This issue also arises at any time you're trying to
write something which has the effect of a <code>return</code> in the calilng
subroutine. You just <b>can't</b> do it. For instance, suppose you want a
sub called <code>abort</code> which displays an error message and ends the calling
sub:</p>
<pre>    if ($badness) {
        abort(&quot;Something yukky happened&quot;);
    }

    sub abort {
        warn @_;
        return 0;
    }</pre>
<p>Uhoh; that return only finishes <code>abort</code> - it doesn't finish the calling
sub. Inlining the sub by means of something like <code>sub abort : inline</code>
won't actually help either, unless it's replaced like a macro. (Yuck.)</p>
<p>One way to fix this would be to have the call stack available from Perl
space. That's roughly what the <code>caller()</code> function does, and that's
there for a good reason. However, if we could get at the call stack via
an array, we could solve the problem like this:</p>
<pre>    sub abort {
        warn @_;
        pop @STACK;
    }</pre>
<p>I would have to stipulate that <code>pop</code> was the only modifying operation
permitted on <code>@STACK</code> - <code>push</code>ing to it would be Evil and Bad and
Wrong, like the <code>goto</code> from the ninth level of hell.</p>
<p>But what should be in each element of <code>@STACK</code>? If we had each element
representing the output of <code>caller()</code>, we could do away with that too.
The following equivalences hold between Perl 5 and Perl 6:</p>
<pre>    Perl 6     =&gt; Perl 5
    $STACK[-1]  = [caller(0)];
    $STACK[-2]  = [caller(1)];
    ...</pre>
<a name='IMPLEMENTATION'></a><h1>IMPLEMENTATION</h1>
<p>Making the call stack available to Perl is easily done, since we do
exactly that in <code>caller()</code>; changing that to a special variable via
whatever becomes of the magic system shouldn't be too difficult.</p>
<p>Modifying it is a little more tricky, but can be done with the same
magic that <code>goto &amp;thing</code> uses.</p>
<a name='REFERENCES'></a><h1>REFERENCES</h1>
<p>RFC 21: Subroutines: Replace <code>wantarray</code> with a generic <code>want</code> function</p>
</div>
